package cn.zjitc.algorithm.sort;

public class Bubble {
    public static void sort(Comparable[] a){
        for (int i = a.length-1; i > 0 ; i--) {
            for (int j = 0; j < i; j++) {
                //比较索引j和索引j+1的值
                if(greater(a[j],a[j+1])){
                    exch(a,j,j+1);
                }
            }

        }
    }

    private static void exch(Comparable[] a, int j, int i) {
        Comparable temp;
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }

    private static boolean greater(Comparable x, Comparable y) {
        return x.compareTo(y)>0;
    }
}
